********************************************************************************
* Chapter 1 The Puzzle of Party Nationalization 
********************************************************************************
* This chapter seeks to address the issue of party nationaslization in
* Latin America, measuring it with the weighted Mustillo's growth-curve model (2017).  

********************************************************************************
* Data descrition
********************************************************************************
* District-Level Electoral Data (Latin America) ###
* Bolivia            (2002 - 2005): MAS, MNR
* Brazil             (1990 - 2014): PT, PSDB, PMDB
* Chile              (1993 - 2013): PSCh, PDC
* El Salvador        (1997 - 2015): FMNL, ARENA
* Mexico             (1997 - 2012): PRD, PRI
* Nicaragua          (1990 - 2006): FSLN, PLC
* Uruguay            (1989 - 2014): FA, PN

********************************************************************************
* TASK 1: Get data
********************************************************************************
import delimited "District-Level Electoral Data (Latin America).xlsx"

********************************************************************************
* TASK 2: Data screening
********************************************************************************
* Data < Create or change data < Other variables - transformation command < Convert variables from string to numeric

********************************************************************************
* TASK 3: Define time variables
********************************************************************************
gen t= TimeA
label var t "Years from the first election for each party"
gen t_2 = t^2
gen t_3 = t^3

gen tt= TimeB
label var tt "Years from the last election for each party"
gen tt_2 = tt^2
gen tt_3 = tt^3

gen pvp=.
gen pvpP=.

********************************************************************************
* TASK 4: Estimate models
********************************************************************************
* Bolivia  	  
local name Movimiento al Socialismo
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Movimiento al Socialismo", mle var fweight(ValidVoteFirst)
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Movimiento al Socialismo", mle var fweight(ValidVoteLast)
  
local name Movimiento Nacionalista Revolucionario
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Movimiento Nacionalista Revolucionario", mle var fweight(ValidVoteFirst) 
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Movimiento Nacionalista Revolucionario", mle var fweight(ValidVoteLast) 
	  

* Brazil
local name Partido dos Trabalhadores
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido dos Trabalhadores", mle var fweight(ValidVoteFirst) 
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido dos Trabalhadores", mle var fweight(ValidVoteLast)
	  
local name Partido da Social Democracia Brasileiro
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido da Social Democracia Brasileiro", mle var fweight(ValidVoteFirst)
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido da Social Democracia Brasileiro", mle var fweight(ValidVoteLast)
	  
local name Partido do Movimiento Democrático Brasileiro
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido do Movimiento Democrático Brasileiro", mle var fweight(ValidVoteLast) 
      xtmixed VoteShare tt || ConstituencyName: if PartyName=="Partido do Movimiento Democrático Brasileiro", mle var fweight(ValidVoteLast)
	  	  
local name Partido dos Trabalhadores
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido dos Trabalhadores", mle var fweight(ValidVoteFirst) 
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido dos Trabalhadores", mle var fweight(ValidVoteLast)
	  
	  
* Chile
local name Partido Socialista de Chile
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido Socialista de Chile", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido Socialista de Chile", mle var fweight(ValidVoteLast)

local name Partido Demócrata Cristiano
      xtmixed VoteShare t t_2 || ConstituencyName: if PartyName=="Partido Demócrata Cristiano", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido Demócrata Cristiano", mle var fweight(ValidVoteLast)
	  

* El Salvador
local name Farabundo Marti National Liberation Front 
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Farabundo Marti National Liberation Front", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Farabundo Marti National Liberation Front", mle var fweight(ValidVoteLast)
	
local name National Republican Alliance 
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="National Republican Alliance", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="National Republican Alliance", mle var fweight(ValidVoteLast)

* Mexico
local name Partido Revolucionario Institucional 
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido Revolucionario Institucional", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido Revolucionario Institucional", mle var fweight(ValidVoteLast)
	
local name National Republican Alliance 
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="National Republican Alliance", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="National Republican Alliance", mle var fweight(ValidVoteLast)	  
	  

* Nicaragua
local name Frente Sandinista de Liberación Nacional
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Frente Sandinista de Liberación Nacional", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Frente Sandinista de Liberación Nacional", mle var fweight(ValidVoteLast)
			 
local name Partido Liberal Constitucionalista 
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido Liberal Constitucionalista", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido Liberal Constitucionalista", mle var fweight(ValidVoteLast)
		  

* Uruguay
local name Frente Amplio
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Frente Amplio", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Frente Amplio", mle var fweight(ValidVoteLast)
		  		  
local name Partido Nacional
      xtmixed VoteShare t t_2 t_3 || ConstituencyName: if PartyName=="Partido Nacional", mle var fweight(ValidVoteFirst)  
      xtmixed VoteShare tt tt_2 tt_3 || ConstituencyName: if PartyName=="Partido Nacional", mle var fweight(ValidVoteLast)
					  
